package com.bestcem.xm.deliver.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.messagecenter.grpc.v1.services.GetMessageTemplateByIdRequest;
import com.bestcem.xm.messagecenter.grpc.v1.services.GetMessageTemplateByIdResponse;
import com.bestcem.xm.messagecenter.grpc.v1.services.MessageCenterTemplate;
import com.bestcem.xm.messagecenter.grpc.v1.services.MessagecenterServiceGrpc;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Component;
import zhongyan.xm.base.v1.Base;

import java.util.Objects;

/**
 * 消息库模块 grpc接口
 *
 * @author xiaoshu.shao <xiaoshu.shao@idiaoyan.com>
 * @version v1.0
 * @date 2023/1/6 11:20
 */
@Slf4j
@Component
public class MessageCenterClient {

    @GrpcClient(GrpcClientNames.DS_QDES)
    private MessagecenterServiceGrpc.MessagecenterServiceBlockingStub messagecenterServiceBlockingStub;

    public MessageCenterTemplate getMessageTemplateById(String id) {
        GetMessageTemplateByIdRequest.Builder builder = GetMessageTemplateByIdRequest.newBuilder();
        builder.setId(id);
        try {
            GetMessageTemplateByIdResponse response = messagecenterServiceBlockingStub.getMessageTemplateById(builder.buildPartial());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Deliver] MessageCenter getMessageTemplateById {} Error; ErrorMsg: {}", id,
                        response);
                return null;
            }
            return response.getMessageCenterTemplate();
        } catch (Exception e) {
            log.error("[Deliver] MessageCenter getMessageTemplateById {};Error;", id);
            return null;
        }
    }
}
